package com.pig4cloud.pigx.contract.constant;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * ContractKLine相关常量
 *
 * @date 2022/8/15 17:24
 */
public interface ContractKLineConstants {

	@Data
	@AllArgsConstructor
	class TimeRangeTypeConfig {
		/**
		 * 日期范围类型（1min、5min、15min、30min、1h、4h、12h、1day、1week、1month）
		 */
		private final String type;
		/**
		 * cron表达式，用来根据当前时间获取下个节点
		 */
		private final String cron;
		/**
		 * 缓存最大条数
		 */
		private final Integer cacheMaxNum;
		/**
		 * 将下个节点作为结束时间逆推开始时间时，要减少的时间
		 */
		private final Integer calendarMinusField;
		/**
		 * 将下个节点作为结束时间逆推开始时间时，要减少的时间
		 */
		private final Integer calendarMinusNum;
		/**
		 * 对应mysql表名
		 */
		private final String tableName;
		/**
		 * 对标币安的K线类型(修复数据使用)
		 */
		private final String biAnLineType;
	}

	String TIME_RANGE_TYPE_1MIN = "1min";
	String TIME_RANGE_TYPE_5MIN = "5min";
	String TIME_RANGE_TYPE_15MIN = "15min";
	String TIME_RANGE_TYPE_30MIN = "30min";
	String TIME_RANGE_TYPE_1H = "1h";
	String TIME_RANGE_TYPE_4H = "4h";
	String TIME_RANGE_TYPE_12H = "12h";
	String TIME_RANGE_TYPE_1DAY = "1day";
	String TIME_RANGE_TYPE_1WEEK = "1week";
	String TIME_RANGE_TYPE_1MONTH = "1month";

	/**
	 * 1min
	 */
	TimeRangeTypeConfig MIN_1 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_1MIN,
			"0 0/1 * * * ?",
			1000,
			Calendar.MINUTE,
			-1,
			"ripple_contract_k_line_1min",
			"1m"
			);
	/**
	 * 5min
	 */
	TimeRangeTypeConfig MIN_5 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_5MIN,
			"0 0/5 * * * ?",
			1000,
			Calendar.MINUTE,
			-5,
			"ripple_contract_k_line_5min",
			"5m"
	);
	/**
	 * 15min
	 */
	TimeRangeTypeConfig MIN_15 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_15MIN,
			"0 0/15 * * * ?",
			1000,
			Calendar.MINUTE,
			-15,
			"ripple_contract_k_line_15min",
			"15m"
	);
	/**
	 * 30min
	 */
	TimeRangeTypeConfig MIN_30 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_30MIN,
			"0 0/30 * * * ?",
			1000,
			Calendar.MINUTE,
			-30,
			"ripple_contract_k_line_30min",
			"30m"
	);
	/**
	 * 1h
	 */
	TimeRangeTypeConfig H_1 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_1H,
			"0 0 0/1 * * ?",
			1000,
			Calendar.HOUR,
			-1,
			"ripple_contract_k_line_1h",
			"1h"
	);
	/**
	 * 4h
	 */
	TimeRangeTypeConfig H_4 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_4H,
			"0 0 0/4 * * ?",
			1000,
			Calendar.HOUR,
			-4,
			"ripple_contract_k_line_4h",
			"4h"
	);
	/**
	 * 12h
	 */
	TimeRangeTypeConfig H_12 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_12H,
			"0 0 0/12 * * ?",
			1000,
			Calendar.HOUR,
			-12,
			"ripple_contract_k_line_12h",
			"12h"
	);
	/**
	 * 1day
	 */
	TimeRangeTypeConfig DAY_1 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_1DAY,
			"0 0 8 1/1 * ?",
			1000,
			Calendar.DATE,
			-1,
			"ripple_contract_k_line_1day",
			"1d"
	);
	/**
	 * 1week
	 */
	TimeRangeTypeConfig WEEK_1 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_1WEEK,
			"0 0 8 ? * 1",
			1000,
			Calendar.DATE,
			-7,
			"ripple_contract_k_line_1week",
			"1w"
	);
	/**
	 * 1month
	 */
	TimeRangeTypeConfig MONTH_1 = new TimeRangeTypeConfig(TIME_RANGE_TYPE_1MONTH,
			"0 0 8 1 * ?",
			1000,
			Calendar.MONTH,
			-1,
			"ripple_contract_k_line_1month",
			"1M"
	);

	/**
	 * 启用的时间范围集合
	 */
	List<TimeRangeTypeConfig> ENABLE_TIME_RANGE_TYPE_LIST = Arrays.asList(
			MIN_1,
			MIN_5,
			MIN_15,
			MIN_30,
			H_1,
			H_4,
			H_12,
			DAY_1,
			WEEK_1,
			MONTH_1
	);

	/**
	 * 执行成交记录的范围
	 */
	List<String> SAVE_TRADE_RANGE_TYPE_LIST = Arrays.asList(
			TIME_RANGE_TYPE_1MIN,
			TIME_RANGE_TYPE_1H
	);

	/**
	 * 根据type查找对应的对象
	 *
	 * @param type 时间范围类型
	 * @return {@link TimeRangeTypeConfig}
	 * @date 2021/11/14
	 * @since 1.0.0
	 */
	static TimeRangeTypeConfig findByTimeRangeType(String type) {
		for (TimeRangeTypeConfig timeRangeTypeConfig : ENABLE_TIME_RANGE_TYPE_LIST) {
			if (timeRangeTypeConfig.getType().equals(type)) {
				return timeRangeTypeConfig;
			}
		}

		return null;
	}

	/**
	 * 判断是否拥有传入的timeRangeType
	 *
	 * @param type 时间范围类型
	 * @return {@link boolean}
	 * @date 2021/11/14
	 * @since 1.0.0
	 */
	static boolean hasTimeRangeType(String type) {
		for (TimeRangeTypeConfig timeRangeTypeConfig : ENABLE_TIME_RANGE_TYPE_LIST) {
			if (timeRangeTypeConfig.getType().equals(type)) {
				return true;
			}
		}

		return false;
	}
}
